package com.ruoyi.ax.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ax.domain.AxReceiveAddress;
import com.ruoyi.ax.service.IAxReceiveAddressService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收货地址维护Controller
 * 
 * @author ruoyi
 * @date 2025-05-07
 */
@RestController
@RequestMapping("/ax/axReceiveAddress")
public class AxReceiveAddressController extends BaseController
{
    @Autowired
    private IAxReceiveAddressService axReceiveAddressService;

    /**
     * 查询收货地址维护列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AxReceiveAddress axReceiveAddress)
    {
        startPage();
        List<AxReceiveAddress> list = axReceiveAddressService.selectAxReceiveAddressList(axReceiveAddress);
        return getDataTable(list);
    }

    /**
     * 导出收货地址维护列表
     */
    @Log(title = "收货地址维护", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AxReceiveAddress axReceiveAddress)
    {
        List<AxReceiveAddress> list = axReceiveAddressService.selectAxReceiveAddressList(axReceiveAddress);
        ExcelUtil<AxReceiveAddress> util = new ExcelUtil<AxReceiveAddress>(AxReceiveAddress.class);
        util.exportExcel(response, list, "收货地址维护数据");
    }

    /**
     * 获取收货地址维护详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(axReceiveAddressService.selectAxReceiveAddressById(id));
    }

    /**
     * 新增收货地址维护
     */
    @Log(title = "收货地址维护", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AxReceiveAddress axReceiveAddress)
    {
        return toAjax(axReceiveAddressService.insertAxReceiveAddress(axReceiveAddress));
    }

    /**
     * app新增收货地址
     */
    @Log(title = "app新增收货地址", businessType = BusinessType.INSERT)
    @PostMapping("/appadd")
    public AjaxResult appadd(@RequestBody AxReceiveAddress axReceiveAddress)
    {
        long Userid=SecurityUtils.getUserId();
        axReceiveAddress.setSysUserId(Userid);
        axReceiveAddressService.insertAxReceiveAddress(axReceiveAddress);
        return success(axReceiveAddress);
    }

    /**
     * app新增收货地址
     */
    @Log(title = "app获取我的收货地址", businessType = BusinessType.INSERT)
    @GetMapping("/getmyaddress")
    public TableDataInfo getmyaddress(AxReceiveAddress axReceiveAddress)
    {
        long Userid=SecurityUtils.getUserId();
        axReceiveAddress.setSysUserId(Userid);
        List<AxReceiveAddress> list = axReceiveAddressService.selectAxReceiveAddressList(axReceiveAddress);
        return getDataTable(list);
    }


    /**
     * app新增收货地址
     */
    @Log(title = "app获取我的收货地址", businessType = BusinessType.INSERT)
    @PostMapping("/setMyDefaultAddress")
    public AjaxResult setMyDefaultAddress(AxReceiveAddress axReceiveAddress)
    {
        long Userid=SecurityUtils.getUserId();
        AxReceiveAddress axReceiveAddressParam=new AxReceiveAddress();
        axReceiveAddressParam.setSysUserId(Userid);
        axReceiveAddressParam.setIsDefault(1l);
        // 将之前设置的默认信息置为1
        axReceiveAddressService.updateAxReceiveAddressByUserId(axReceiveAddressParam);
        axReceiveAddress.setIsDefault(2l);
        return toAjax(axReceiveAddressService.updateAxReceiveAddress(axReceiveAddress));
    }




    /**
     * 修改收货地址维护
     */
    @Log(title = "收货地址维护", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AxReceiveAddress axReceiveAddress)
    {
        return toAjax(axReceiveAddressService.updateAxReceiveAddress(axReceiveAddress));
    }

    /**
     * 删除收货地址维护
     */
    @Log(title = "收货地址维护", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(axReceiveAddressService.deleteAxReceiveAddressByIds(ids));
    }
}
